package com.isoftstone.util;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.codec.Base64;


/**
 * 3DES加密工具类
 * 
 * @author 
 * @date 2014-7-9
 */
public class DES3 {
	// 密钥
	private final static String secretKey = "x1234g6789abcd012345w789";
	// 向量
	private final static String iv = "12345678";
	// 加解密统一使用的编码方式
	private final static String encoding = "UTF-8";

	public static void main(String[] args) throws Exception {
		// 实现3DES加密算法，工作模式CBC，填充模式PKCS5
		String str = "18583221860";
		String encstr = encode(str);
		System.out.println("加密后：" + encstr);
		System.out.println("解密后：" + decode(encstr));
	}

	/**
	 * 3DES加密
	 * 
	 * @param plainText
	 *            普通文本
	 * @return
	 * @throws Exception
	 */
	public static String encode(String plainText) throws Exception {
		return encode(plainText, secretKey);
	}

	public static String encode(String plainText, String secretKey)
			throws Exception {
		Key deskey = null;
		DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
		SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
		deskey = keyfactory.generateSecret(spec);

		Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
		IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
		cipher.init(Cipher.ENCRYPT_MODE, deskey,ips);
		byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
		return new String ( Base64.encode(encryptData));
	}

	/**
	 * 3DES解密
	 * 
	 * @param encryptText
	 *            加密文本
	 * @return
	 * @throws Exception
	 */
	public static String decode(String plainText) throws Exception {
		if (StringUtils.isBlank(plainText)) {
			return "";
		}
		return decode(plainText, secretKey);
	}

	public static String decode(String encryptText, String secretKey)
			throws Exception {
		if (StringUtils.isBlank(encryptText)||StringUtils.isBlank(secretKey)) {
			return "";
		}
		Key deskey = null;
		DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
		SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
		deskey = keyfactory.generateSecret(spec);
		Cipher cipher = Cipher.getInstance("desede/CBC/NoPadding");
		IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
	    cipher.init(Cipher.DECRYPT_MODE, deskey,ips);

		byte[] decryptData = cipher.doFinal(Base64.decode(encryptText));
		
		return new String(decryptData, encoding).trim();
	}
}